var userList = new Array();
var conversation = new Array();


function Message(senderId, receiverId, content){
	this.sId = senderId;
	this.rId = receiverId;
	this.content = content;
	this.date = new Date();
}

function remoteUserLogout(ru){
	var u = new User(ru.userId, ru.name, ru.level);
	if(userList[u.userId]){
		userList[u.userId] = null;
		$('#'+u.userId).remove();
	}	
}

function getLiforUser(u){

	var img = '<button id="btn'+u.userId+'" onClick="popover('+u.userId+')" type="button" class="btn btn-default" data-toggle="popover" title="Information" data-placement="right" data-content="ok" data-original-title="Information"><span class="glyphicon glyphicon-chevron-right"></span></button>';
	return '<p id="'+u.userId+'" style="margin-bottom:10px;">'+img+' <span onclick="changeChatUser('+u.userId+');"> '+u.name+'</span></p>';	
}

function remoteUserLogin(ru){
	var u = new User(ru.userId, ru.name, ru.level, ru.dCounter, ru.vCounter, ru.ltCounter, ru.ntGame, ru.lastConx, ru.ntConx);
	if(!userList[u.userId]){
		userList[u.userId] = u;
		$("#userList").prepend(getLiforUser(u));
	}	
}

function receiveMessage(message){
	 
	var msg = new Message(message.sId, message.rId, message.content);
	msg.date = message.date;
	
	if( conversation[msg.sId]){
		conversation[msg.sId].push(msg);
		
	}else{
		conversation[msg.sId] = new Array();
		conversation[msg.sId].push(msg);
	}
	var key = $("#chatWith").attr("key");
	if( key == msg.sId){
		$("#chatMessagesPanel").prepend('<p class="chatMessageR">'+msg.content+'</p>');
	}else{
		$('#'+msg.sId).css("background-color","#E1EBC5").css("color","white");
	}
}

function sendChatMessage(message){
	if( message.val().length > 0 ){
		var key = $("#chatWith").attr("key");;
		var msg = new Message(user.userId, key, message.val());
		if( conversation[key]){
			conversation[key].push(msg);
			
		}else{
			conversation[key] = new Array();
			conversation[key].push(msg);
		}
		$("#chatMessagesPanel").prepend('<p class="chatMessageS">'+msg.content+'</p>');
		message.val("");
		
		$.get("/chat", {"ACTION":Cst.SEND_MESSAGE, "USER_ID":user.userId, "MESSAGE":JSON.stringify(msg)})
		.done(function(){
			//alert("Success sent! User id="+user.userId+"  Message in json : "+JSON.stringify(msg))
		})
		.fail(function(){
			alert("Echec d'envoi du message : "+msg.content +" || "+userList[key].name);
		});
	}
}

function getUserList(user){
	$.get("/onlineCheckers", {"ACTION":Cst.CONNECTED_USER_LIST, "USER_ID":user.userId})
	.fail(function(){
		alert("User list : Erreur d'initialisation");
	});
}

function setUserList(d){
	var ul = $("#userList");
	ul.empty();
	for(var key in d ){
		if(key == user.userId ){ 
			user = new User(key, d[key]["name"], d[key]["level"], d[key]["dCounter"], d[key]["vCounter"], d[key]["ltCounter"], d[key]["ntGame"], d[key]["lastConx"], d[key]["ntConx"]);
			var ui = $("#userInfo");
			ui.empty();
			ui.prepend('<p id="tk'+user.userId+'"><button title="Obtenir un token!" type="button" class="btn btn-default" onClick="getGameToken();" ><img width="30" height="30"src="../images/get-token.png"/></button></p>');
			ui.prepend("<p>Nombre defaite : "+user.dCounter+"</p>");
			ui.prepend("<p>Niveau : "+ user.level+"</p>");
			ui.prepend("<p>Nom : "+user.name+"</p>");
		}else{
			userList[key] = new User(key, d[key]["name"], d[key]["level"], d[key]["dCounter"], d[key]["vCounter"], d[key]["ltCounter"], d[key]["ntGame"], d[key]["lastConx"], d[key]["ntConx"]);
			ul.prepend(getLiforUser(userList[key]));
		}
	}
}

function changeChatUser(key){
	$("#chatMessagesPanel").empty();
	if( key != "123321"){
		$("#chatWith").html("Chat avec "+userList[key].name);
		$("#chatWith").attr("key",key);
		$("#messageToSend").prop('disabled', false);
		
		if(conversation[key]){
			$('#'+key).css("background-color","").css("color","");
			for(var i = 0; i < conversation[key].length; i++ ){
				var m = conversation[key][i];
				if( m.sId == user.userId ){
					$("#chatMessagesPanel").prepend('<p class="chatMessageS">'+conversation[key][i].content+'</p>');
				}else
					$("#chatMessagesPanel").prepend('<p class="chatMessageR">'+conversation[key][i].content+'</p>');
			}
		}
	}else{
		$("#messageToSend").prop('disabled', true);
		$("#chatWith").html("Pas de chat");
	}
	$("#messageToSend").val("").focus();
}